<?php
namespace App\Repositories;

use App\Models\Occupation;
use Exception;

class OccupationRepository
{
    public function getLists($filter = [], $tree = true)
    {
        $cache_key = "cache_occupation_list_" . md5(implode("_",array_values($filter)));
        $res = \Cache::get($cache_key);

        if(empty($res)){
            $res = Occupation::query()
            ->where($filter)
            ->orderBy("id", "asc")
            ->orderBy("sort", "desc")
            ->get(['id',"pid",'title'])
            ->toArray();

            if($tree){
                $res = list_to_tree($res,"id", "pid");
            }
            \Cache::set($cache_key, $res);
        }

        return $res;
    }
}
